<!DOCTYPE html>
<html class="x-admin-sm">

    <head>
        <meta charset="UTF-8">
        <title>修改模板</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<!--        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />-->
        <link rel="stylesheet" href="./css/font.css">
        <link rel="stylesheet" href="./css/xadmin.css">
        <script src="./lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="./js/xadmin.js"></script>
        <script type="text/javascript" src="./js/jquery.min.js"></script>
        <script type="text/javascript" src="./data/sensor.js"></script>
        <!--[if lt IE 9]>
          <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
          <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
 <style>
     #tab_ledType tr td{
         padding: 5px;
         padding-left: 10px;
     }
 </style>
    </head>

    <body>
<!--        <div class="x-nav">-->
<!--            <span class="layui-breadcrumb">-->
<!--                <a href="">首页</a>-->
<!--                <a href="">系统设置</a>-->
<!--                <a>-->
<!--                    <cite>账户管理</cite></a>-->
<!--            </span>-->
<!--            <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" onclick="location.reload()" title="刷新">-->
<!--                <i class="layui-icon layui-icon-refresh" style="line-height:30px"></i>-->
<!--            </a>-->
<!--        </div>-->
        <div class="layui-fluid">
<!--            <div class="layui-row layui-col-space15">-->
                <div class="layui-col-md12">
                    <div class="layui-card">
                        <div class="layui-card-body ">
                            <textarea id="input_xml" style="width: 100%;height: 600px;"></textarea>
                            <button  class="layui-btn-normal layui-btn"  onclick="formartXml()">格式化</button>
                            <button  class="layui-btn-normal layui-btn"  onclick="saveXML()">保存</button>
                        </div>
                    </div>
                </div>
<!--            </div>-->
        </div>

    </body>
    <script type="text/html" id="toolbarMenu">
        <div class = "layui-btn-container" >
            <!--            <button class = "layui-btn layui-btn-sm" lay-event = "getCheckData" > 获取选中行数据 </button>-->
            <!--            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button >-->
            <button class = "layui-btn layui-btn-sm" lay-event = "addSensor" onclick="xadmin.open('新增账号','./accountList-add.html',600,400)" > 新增账号</button>
<!--            <button class = "layui-btn layui-btn-danger" lay-event = "delCheckData" > <i class="layui-icon"></i>删除选中 </button>-->
        </div >
    </script>
    <script type="text/html" id="toolbarDemo">
        <div class = "layui-btn-container" >
<!--            <button class = "layui-btn layui-btn-sm" lay-event = "getCheckData" > 获取选中行数据 </button>-->
<!--            <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button >-->
<!--            <button class = "layui-btn layui-btn-sm" lay-event = "isAll" > 验证是否全选</button>-->
            <button class = "layui-btn layui-btn-danger"   lay-event = "delete" > 删除</button>
            <button class="layui-btn layui-btn-sm"  lay-event = "editInfo" > 修改</button>
         </div >
    </script>


    <script type="text/javascript">
        function getQueryVariable(variable)
        {
            var query = window.location.search.substring(1);
            var vars = query.split("&");
            for (var i=0;i<vars.length;i++) {
                var pair = vars[i].split("=");
                if(pair[0] == variable){return pair[1];}
            }
            return(false);
        }
        // setInterval(function(){
        //    // getData();
        // },1000);

        let ledInfo;
        let curInfo;
        $(function () {
            let pid=getQueryVariable("id");
            var initLedInfo=sensorServer.getContentList({"pid":pid,"page":1,"rows":1000});
            debugger;
            curInfo=initLedInfo.data[0];
            $("#input_xml").val(curInfo.xmlContent);
        });

        function saveXML(){
            curInfo.xmlContent=$("#input_xml").val();
            let result=sensorServer.updateContentInfo(curInfo);
            if(result==1) {
                layer.alert("修改成功", {
                        icon: 6
                    },
                    function () {
                        //关闭当前frame
                        xadmin.close();
                        // 可以对父窗口进行刷新
                        xadmin.father_reload();
                    });
            }else{
                layer.alert("修改失败", {
                        icon: 5
                    },
                    function () {
                        //关闭当前frame
                        xadmin.close();

                        // 可以对父窗口进行刷新
                        xadmin.father_reload();
                    });
            }
        }



        (function() {

            function createShiftArr(step) {

                var space = '    ';

                if ( isNaN(parseInt(step)) ) {  // argument is string
                    space = step;
                } else { // argument is integer
                    switch(step) {
                        case 1: space = ' '; break;
                        case 2: space = '  '; break;
                        case 3: space = '   '; break;
                        case 4: space = '    '; break;
                        case 5: space = '     '; break;
                        case 6: space = '      '; break;
                        case 7: space = '       '; break;
                        case 8: space = '        '; break;
                        case 9: space = '         '; break;
                        case 10: space = '          '; break;
                        case 11: space = '           '; break;
                        case 12: space = '            '; break;
                    }
                }

                var shift = ['\n']; // array of shifts
                for(ix=0;ix<100;ix++){
                    shift.push(shift[ix]+space);
                }
                return shift;
            }

            function vkbeautify(){
                this.step = '    '; // 4 spaces
                this.shift = createShiftArr(this.step);
            };

            vkbeautify.prototype.xml = function(text,step) {

                var ar = text.replace(/>\s{0,}</g,"><")
                        .replace(/</g,"~::~<")
                        .replace(/\s*xmlns\:/g,"~::~xmlns:")
                        .replace(/\s*xmlns\=/g,"~::~xmlns=")
                        .split('~::~'),
                    len = ar.length,
                    inComment = false,
                    deep = 0,
                    str = '',
                    ix = 0,
                    shift = step ? createShiftArr(step) : this.shift;

                for(ix=0;ix<len;ix++) {
                    // start comment or <![CDATA[...]]> or <!DOCTYPE //
                    if(ar[ix].search(/<!/) > -1) {
                        str += shift[deep]+ar[ix];
                        inComment = true;
                        // end comment  or <![CDATA[...]]> //
                        if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1 || ar[ix].search(/!DOCTYPE/) > -1 ) {
                            inComment = false;
                        }
                    } else
                        // end comment  or <![CDATA[...]]> //
                    if(ar[ix].search(/-->/) > -1 || ar[ix].search(/\]>/) > -1) {
                        str += ar[ix];
                        inComment = false;
                    } else
                        // <elm></elm> //
                    if( /^<\w/.exec(ar[ix-1]) && /^<\/\w/.exec(ar[ix]) &&
                        /^<[\w:\-\.\,]+/.exec(ar[ix-1]) == /^<\/[\w:\-\.\,]+/.exec(ar[ix])[0].replace('/','')) {
                        str += ar[ix];
                        if(!inComment) deep--;
                    } else
                        // <elm> //
                    if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) == -1 && ar[ix].search(/\/>/) == -1 ) {
                        str = !inComment ? str += shift[deep++]+ar[ix] : str += ar[ix];
                    } else
                        // <elm>...</elm> //
                    if(ar[ix].search(/<\w/) > -1 && ar[ix].search(/<\//) > -1) {
                        str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix];
                    } else
                        // </elm> //
                    if(ar[ix].search(/<\//) > -1) {
                        str = !inComment ? str += shift[--deep]+ar[ix] : str += ar[ix];
                    } else
                        // <elm/> //
                    if(ar[ix].search(/\/>/) > -1 ) {
                        str = !inComment ? str += shift[deep]+ar[ix] : str += ar[ix];
                    } else
                        // <? xml ... ?> //
                    if(ar[ix].search(/<\?/) > -1) {
                        str += shift[deep]+ar[ix];
                    } else
                        // xmlns //
                    if( ar[ix].search(/xmlns\:/) > -1  || ar[ix].search(/xmlns\=/) > -1) {
                        str += shift[deep]+ar[ix];
                    }

                    else {
                        str += ar[ix];
                    }
                }

                return  (str[0] == '\n') ? str.slice(1) : str;
            }

            window.vkbeautify = new vkbeautify();

        })();

        function formartXml() {

            var xmlData =$('#input_xml').val(); //'<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don\'t forget me this weekend!</body></note>';
            xmlData = vkbeautify.xml(xmlData);
            $('#input_xml').val(xmlData);
        }

    </script>


</html>
